import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { IView } from "../../mvc/interfaces/IView";
import { ShopMallModule } from "../../../modules/ShopMallModule";
import { WindowEvent } from "../../component/window/WindowEvent";
import { DataEvent } from "../events/DataEvent";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { SMoney } from "../../../Message/Game/SMoney";
import { ShopItemInfo } from "../resource/info/ShopItemInfo";
import { GameProxy } from "../mvc/GameProxy";
import { SPanicBuyItemMsg } from "../../../Message/Game/SPanicBuyItemMsg";
import { SPanicBuyRefreshMsg } from "../../../Message/Game/SPanicBuyRefreshMsg";
import { ItemData } from "../resource/info/ItemData";
import { ItemsUtil } from "../view/shortcuts/item/ItemsUtil";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { SoundManager } from "../../common/sound/SoundManager";
import { SoundTypeConst } from "../../common/sound/SoundTypeConst";
import { PreviewWindow } from "../view/shopMall/PreviewWindow";
import { ShopRule } from "../rules/ShopRule";
import { BuyCoinWindow } from "../view/common/BuyCoinWindow";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { IShopMallModule } from "../../../modules/interfaces/IShopMallModule";
type int = number;
//class ShopMallController
    
    export  class ShopMallController extends Controller
    {
        constructor()
        {
            
            super();Dispatcher.addEventListener(EventName.ShopMallBuyItem, this.buyItemHandler);
            Dispatcher.addEventListener(EventName.WareHouseBuyItem, this.buyItemHandler);
            Dispatcher.addEventListener(EventName.ShopMallBuyItemAndUse, this.buyItemAndUseHandler);
            Dispatcher.addEventListener(EventName.ShopMallBuyItemSuccess, this.onShopMallBuyItemSuccess);
            Dispatcher.addEventListener(EventName.PanicBuyItem, this.onPanicBuyItemHandler);
            Dispatcher.addEventListener(EventName.GetPanicBuyConfig, this.onGetPanicBuyConfigHandler);
            Dispatcher.addEventListener(EventName.GetPanicBuyConfigSuccess, this.onGetPanicBuyConfigSuccessHandler);
            Dispatcher.addEventListener(EventName.ClosePanicBuy, this.onClosePanicBuyHandler);
            Dispatcher.addEventListener(EventName.PanicBuyItemUpdate, this.onPanicBuyItemUpdateHandler);
            Dispatcher.addEventListener(EventName.PanicBuyRefresh, this.onPanicBuyRefreshHandler);
            Dispatcher.addEventListener(EventName.GuidOpenShopMall, this.onGuidOpenShopMallHandler);
            Dispatcher.addEventListener(EventName.ShopMallOpen, this.onShopMallOpenHandler);
            Dispatcher.addEventListener(EventName.PetMountPreview, this.onPetMountPreviewHandler);
            Dispatcher.addEventListener(EventName.VIPOpenShopMall, this.onVIPOpenShopMallHandler);
            Dispatcher.addEventListener(EventName.ShopMallShowToTab, this.onShopMallShowToTabHandler);
            Dispatcher.addEventListener(EventName.ShopMallGetCostReturn, this.onShopMallGetCostReturnHandler);
            Dispatcher.addEventListener(EventName.ShopMallAutoBuyCoin, this.onShopMallAutoBuyCoinHandler);
            Dispatcher.addEventListener(EventName.AcitveTimeUpdate, this.onAcitveTimeUpdateHandler);
            return;
        }

        public /* override */ get view(): IView
        {
            return this.initView();
        }

        protected /* override */ initView(): IView
        {
            if (null == this._shopMallModule) 
            {
                this._shopMallModule = new ShopMallModule();
                this._shopMallModule.addEventListener(WindowEvent.SHOW, this.onAddedToStageHandler);
            }
            return this._shopMallModule;
        }

        /* internal  */onAddedToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            this._shopMallModule.showTabByShopCode(0);
            this.onMoneyUpdateHandler();
            Dispatcher.dispatchEvent(new DataEvent(EventName.GetPanicBuyConfig));
            this.onShopMallCostReturnUpdateHandler(cache.role.costReturn);
            return;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.ShopMallCostReturnUpdate, this.onShopMallCostReturnUpdateHandler);
            return;
        }

        /* internal  */onMoneyUpdateHandler(arg1: SMoney=null): void
        {
            if (this._shopMallModule && this._shopMallModule.isHide == false) 
            {
                this._shopMallModule.update(cache.role.money);
            }
            return;
        }

        /* internal  */onShopMallCostReturnUpdateHandler(arg1: int): void
        {
            var loc1=arg1 / 100;
            if (this._shopMallModule) 
            {
                this._shopMallModule.updateCostReturn(loc1.toFixed(2));
            }
            return;
        }

        /* internal  */buyItemHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["shopItemInfo"] as ShopItemInfo;
            var loc2=arg1.data["amount"] as int;
            var loc3=arg1.data["callBack"] as Function;
            GameProxy.shopProxy.buyItem(0, loc1.tshopSell.shopCode, loc1.itemInfo.item.code, loc2, loc3);
            return;
        }

        /* internal  */buyItemAndUseHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["shopItemInfo"] as ShopItemInfo;
            var loc2=arg1.data["amount"] as int;
            GameProxy.shopProxy.buyItemAndUse(0, loc1.tshopSell.shopCode, loc1.itemInfo.item.code, loc2);
            return;
        }

        /* internal  */onPanicBuyItemHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["itemCode"] as int;
            var loc2=arg1.data["amount"] as int;
            GameProxy.shopProxy.panicBuyItem(loc1, loc2);
            return;
        }

        /* internal  */onGetPanicBuyConfigHandler(arg1: DataEvent): void
        {
            GameProxy.shopProxy.getPanicBuyConfig();
            return;
        }

        /* internal  */onGetPanicBuyConfigSuccessHandler(arg1: DataEvent): void
        {
            if (this._shopMallModule) 
            {
                this._shopMallModule.updatePreferenPanel(cache.shop.refreshMsg);
            }
            return;
        }

        /* internal  */onPanicBuyItemUpdateHandler(arg1: DataEvent): void
        {
            var loc1=null;
            if (this._shopMallModule) 
            {
                loc1 = arg1.data as SPanicBuyItemMsg;
                this._shopMallModule.refreshDataProvider(loc1);
            }
            return;
        }

        /* internal  */onPanicBuyRefreshHandler(arg1: DataEvent): void
        {
            var loc1=null;
            if (this._shopMallModule) 
            {
                loc1 = arg1.data as SPanicBuyRefreshMsg;
                this._shopMallModule.refreshDataProvider(loc1);
            }
            return;
        }

        /* internal  */onClosePanicBuyHandler(arg1: DataEvent): void
        {
            GameProxy.shopProxy.closePanicBuyPanel();
            return;
        }

        /* internal  */onShopMallBuyItemSuccess(arg1: DataEvent): void
        {
            var loc1="";
            var loc2=arg1.data["itemCode"] as int;
            var loc3=arg1.data["amount"] as int;
            var loc4;
            if (loc4 = new ItemData(loc2)) 
            {
                loc1 = ItemsUtil.getItemName(loc4);
            }
            if (ItemsUtil.isMedal(loc4)) 
            {
                MsgManager.showRollTipsMsg(Language.getString(53097) + loc1);
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(53001, loc3) + loc1);
            }
            SoundManager.instance.soundPlay(SoundTypeConst.GoodsTrade);
            return;
        }

        /* internal  */onPetMountPreviewHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["type"] as int;
            var loc2=arg1.data["code"] as int;
            if (this._previewWindow == null) 
            {
                this._previewWindow = new PreviewWindow();
            }
            this._previewWindow.clear();
            this._previewWindow.updatePetModel(loc2, loc1);
            this._previewWindow.show();
            return;
        }

        /* internal  */onShopMallOpenHandler(arg1: DataEvent): void
        {
            this.view.show();
            return;
        }

        public onGuidOpenShopMallHandler(arg1: DataEvent): void
        {
            this.onShopMallOpenHandler(null);
            this._shopMallModule.showGuideTab();
            return;
        }

        /* internal  */onVIPOpenShopMallHandler(arg1: DataEvent): void
        {
            if (cache.vip.isVIP) 
            {
                this.onShopMallOpenHandler(null);
                this._shopMallModule.showTabByShopCode(ShopRule.ShopMallVip);
            }
            return;
        }

        /* internal  */onShopMallShowToTabHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            this.onShopMallOpenHandler(null);
            this._shopMallModule.showTabByShopCode(loc1);
            return;
        }

        /* internal  */onShopMallGetCostReturnHandler(arg1: DataEvent): void
        {
            GameProxy.shopProxy.getCostReturn();
            return;
        }

        /* internal  */onShopMallAutoBuyCoinHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            var loc2=loc1 % 1000 != 0 ? int(loc1 / 1000) + 1 : loc1 / 1000;
            var loc3=loc1 % 6000 != 0 ? int(loc1 / 6000) + 1 : loc1 / 6000;
            if (this._buyCoinWindow == null) 
            {
                this._buyCoinWindow = BuyCoinWindow.instance;
            }
            this._buyCoinWindow.updateBuyNum(loc2, loc3);
            this._buyCoinWindow.show();
            return;
        }

        /* internal  */onAcitveTimeUpdateHandler(arg1: DataEvent): void
        {
            cache.shop.isShopMallDiscount = cache.active.isActiveInEffect(EActiveType._EActiveTypeShopDiscount);
            return;
        }

        private /* var */_shopMallModule: IShopMallModule;

        private /* var */_previewWindow: PreviewWindow;

        private /* var */_buyCoinWindow: BuyCoinWindow;
    }
